
#include "StdAfx.h"
//#include "AbstractItemView.h"
//#include "AbstractItemModel.h"
//#include "AbstractItemDelegate.h"

namespace DuiLib
{

	CAbstractItemView::CAbstractItemView()
	{
		m_Model = NULL;
		m_GlobalDelegate = NULL;

	}
	CAbstractItemView::~CAbstractItemView()
	{
		DeleteAllDelegates();
	}

	CAbstractItemModel* CAbstractItemView::model() const
	{
		return m_Model;
	}

	void CAbstractItemView::setModel(CAbstractItemModel* pModel)
	{
		if (!pModel)
		{
			return;
		}

		m_Model = pModel;

		m_Model->dataChanged.connect(this, &CAbstractItemView::onDataChanged);
		m_Model->headerDataChanged.connect(this, &CAbstractItemView::onHeaderDataChanged);

		m_Model->rowsBeforeInsert.connect(this, &CAbstractItemView::onRowsBefroreInsert);
		m_Model->rowsInserted.connect(this, &CAbstractItemView::onRowsInserted);

		m_Model->rowsBeforeRemove.connect(this, &CAbstractItemView::onRowsBefroreRemove);
		m_Model->rowsRemoved.connect(this, &CAbstractItemView::onRowsRemoved);

		m_Model->columnsBeforeInsert.connect(this, &CAbstractItemView::onColumnBeforeInsert);
		m_Model->columnsInserted.connect(this, &CAbstractItemView::onColumnInserted);

		m_Model->columnsBeforeRemove.connect(this, &CAbstractItemView::onColumnBeforeRemove);
		m_Model->columnsRemoved.connect(this, &CAbstractItemView::onColumnRemoved);

		m_Model->allRemoved.connect(this, &CAbstractItemView::onRemoveAll);
	}


	void CAbstractItemView::setItemDelegate(CAbstractItemDelegate* pDelegate)
	{
		if (pDelegate)
		{
			pDelegate->setModel(model());

			m_GlobalDelegate = pDelegate;
		}

	}

	CAbstractItemDelegate* CAbstractItemView::itemDelegate() const
	{
		return m_GlobalDelegate;
	}

	void CAbstractItemView::setItemDelegateForRow(int row, CAbstractItemDelegate* pDelegate)
	{
		if (pDelegate)
		{
			pDelegate->setModel(model());

			m_RowDelegates[row] = pDelegate;
		}
	}

	CAbstractItemDelegate* CAbstractItemView::itemDelegateForRow(int row) const
	{
		auto it = m_RowDelegates.find(row);
		if (it == m_RowDelegates.end())
		{
			return NULL;
		}

		return it->second;
	}

	void CAbstractItemView::setItemDelegateForColumn(int column, CAbstractItemDelegate* pDelegate)
	{
		if (pDelegate)
		{
			pDelegate->setModel(model());

			m_ColumnDelegates[column] = pDelegate;
		}
	}

	CAbstractItemDelegate* CAbstractItemView::itemDelegateForColumn(int column) const
	{
		auto it = m_ColumnDelegates.find(column);
		if (it == m_ColumnDelegates.end())
		{
			return NULL;
		}

		return it->second;
	}

	CAbstractItemDelegate* CAbstractItemView::itemDelegate(const CModelIndex &index) const
	{
		CAbstractItemDelegate* pDelegte = NULL;
		pDelegte = itemDelegateForColumn(index.column());
		if (pDelegte)
		{
			return pDelegte;
		}

		pDelegte = itemDelegateForRow(index.row());
		if (pDelegte)
		{
			return pDelegte;
		}

		return itemDelegate();
	}

	void CAbstractItemView::onHeaderDataChanged(int section, Orientation orient,
		const CVar& oldValue, const CVar& newValue)
	{
	}

	void CAbstractItemView::onRowsBefroreInsert(int row, int count, const CModelIndex& parent)
	{
	}
	void CAbstractItemView::onRowsInserted(int row, int count, const CModelIndex& parent)
	{
	}

	void CAbstractItemView::onColumnBeforeInsert(int col, int count, const CModelIndex& parent)
	{
	}
	void CAbstractItemView::onColumnInserted(int col, int count, const CModelIndex& parent)
	{
	}

	void CAbstractItemView::onRowsBefroreRemove(int row, int count, const CModelIndex& parent)
	{
	}
	void CAbstractItemView::onRowsRemoved(int row, int count, const CModelIndex& parent)
	{
	}

	void CAbstractItemView::onColumnBeforeRemove(int col, int count, const CModelIndex& parent)
	{
	}
	void CAbstractItemView::onColumnRemoved(int col, int count, const CModelIndex& parent)
	{
	}

	void CAbstractItemView::onRemoveAll(int rowCount)
	{
	}

	void CAbstractItemView::DeleteAllDelegates()
	{
		SAFE_DELETE(m_GlobalDelegate);

		for (auto d : m_RowDelegates)
		{
			SAFE_DELETE(d.second);
		}
		m_RowDelegates.clear();

		for (auto d : m_ColumnDelegates)
		{
			SAFE_DELETE(d.second);
		}
		m_ColumnDelegates.clear();
	}

}